package com.fsnbigdata.domain;

import com.fsnbigdata.util.ChineseInital;
import io.springlets.format.EntityFormat;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Version;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.annotations.Index;
import org.hibernate.validator.constraints.Length;
import org.springframework.roo.addon.javabean.annotations.RooEquals;
import org.springframework.roo.addon.javabean.annotations.RooJavaBean;
import org.springframework.roo.addon.javabean.annotations.RooToString;
import org.springframework.roo.addon.jpa.annotations.entity.JpaRelationType;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaEntity;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaRelation;
import org.springframework.util.Assert;

/**
 * = Organism
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJavaBean
@RooToString
@RooJpaEntity(entityFormatExpression = "#{name}")
@RooEquals(isJpaEntity = true)
@Entity
@EntityFormat("#{name}")
public class Organism {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Version
    private Integer version;

    /**
     * 物种名
     */
    @NotNull
    @Size(min = 1, max = 255,message ="物种名称信息内容长度必须在0-255个字符之间")
    @Index(name="organism_name")
    private String name;

    /**
     * 物种拉丁名
     */
    @NotNull
    @Size(min = 1, max = 255,message ="物种拉丁名信息内容长度必须在0-255个字符之间")
    private String latinName;

    /**
     * 拼音首字母
     */
    @Index(name="Organism_initial")
    private String initial;

    /**
     * 异名
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String alias;

    /**
     * 定名人及定名时间
     */
    @Length(min = 0, max = 255,message ="物种定名人及定名时间信息内容长度必须在0-255个字符之间")
    private String namedInfo;

    /**
     * 生境,栖息地
     */
    @Length(min = 0, max = 255,message ="物种生境,栖息地信息内容长度必须在0-255个字符之间")
    private String habitat;

    /**
     * 特有性
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String endemism;

    /**
     * 形态特征
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String characteristic;

    /**
     * 利用价值
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String useValues;

    /**
     * 标本存放地点及概况
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String specimenSummary;

    /**
     * 创建者
     */
    @Length(min = 0, max = 255,message ="物种创建者信息内容长度必须在0-255个字符之间")
    private String creator;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "organism", orphanRemoval = true)
    @OrderBy("country, province, city")
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @Valid
    private Set<Distribution> distribution = new HashSet<Distribution>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "organism", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<OrganismSpecimen> specimens = new HashSet<OrganismSpecimen>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "organism", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<OrganismDocument> documents = new HashSet<OrganismDocument>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @ManyToMany(cascade = { javax.persistence.CascadeType.REFRESH, javax.persistence.CascadeType.PERSIST }, fetch = FetchType.EAGER, mappedBy = "organisms")
    @RooJpaRelation(type = JpaRelationType.AGGREGATION)
    @OrderBy("docYear")
    @Valid
    private Set<Document> docs = new HashSet<Document>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "organism", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<OrganismImage> images = new HashSet<OrganismImage>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "organism", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<OrganismVideo> videos = new HashSet<OrganismVideo>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "organism", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<Taxonomy> taxonomy = new HashSet<Taxonomy>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "organism", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<ConservationStatus> conservationStatus = new HashSet<ConservationStatus>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "organism", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<Germplasm> germplasm = new HashSet<Germplasm>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * This `equals` implementation is specific for JPA entities and uses
     * the entity identifier for it, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @param obj
     * @return Boolean
     */
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        // instanceof is false if the instance is null
        if (!(obj instanceof Organism)) {
            return false;
        }
        return getId() != null && Objects.equals(getId(), ((Organism) obj).getId());
    }

    /**
     * This `hashCode` implementation is specific for JPA entities and uses a fixed `int` value to be able
     * to identify the entity in collections after a new id is assigned to the entity, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @return Integer
     */
    public int hashCode() {
        return 31;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Long
     */
    public Long getId() {
        return this.id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Integer
     */
    public Integer getVersion() {
        return this.version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param version
     */
    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getName() {
        return this.name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getLatinName() {
        return this.latinName;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param latinName
     */
    public void setLatinName(String latinName) {
        this.latinName = latinName;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getInitial() {
        return this.initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param initial
     */
    public void setInitial(String initial) {
        this.initial = initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getAlias() {
        return this.alias;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param alias
     */
    public void setAlias(String alias) {
        this.alias = alias;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getNamedInfo() {
        return this.namedInfo;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param namedInfo
     */
    public void setNamedInfo(String namedInfo) {
        this.namedInfo = namedInfo;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getHabitat() {
        return this.habitat;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param habitat
     */
    public void setHabitat(String habitat) {
        this.habitat = habitat;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getEndemism() {
        return this.endemism;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param endemism
     */
    public void setEndemism(String endemism) {
        this.endemism = endemism;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCharacteristic() {
        return this.characteristic;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param characteristic
     */
    public void setCharacteristic(String characteristic) {
        this.characteristic = characteristic;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getUseValues() {
        return this.useValues;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param useValues
     */
    public void setUseValues(String useValues) {
        this.useValues = useValues;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getSpecimenSummary() {
        return this.specimenSummary;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param specimenSummary
     */
    public void setSpecimenSummary(String specimenSummary) {
        this.specimenSummary = specimenSummary;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCreator() {
        return this.creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param creator
     */
    public void setCreator(String creator) {
        this.creator = creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<Distribution> getDistribution() {
        return this.distribution;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param distribution
     */
    public void setDistribution(Set<Distribution> distribution) {
        this.distribution = distribution;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<OrganismSpecimen> getSpecimens() {
        return this.specimens;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param specimens
     */
    public void setSpecimens(Set<OrganismSpecimen> specimens) {
        this.specimens = specimens;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<OrganismDocument> getDocuments() {
        return this.documents;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param documents
     */
    public void setDocuments(Set<OrganismDocument> documents) {
        this.documents = documents;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<Document> getDocs() {
        return this.docs;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param docs
     */
    public void setDocs(Set<Document> docs) {
        this.docs = docs;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<OrganismImage> getImages() {
        return this.images;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param images
     */
    public void setImages(Set<OrganismImage> images) {
        this.images = images;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<OrganismVideo> getVideos() {
        return this.videos;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param videos
     */
    public void setVideos(Set<OrganismVideo> videos) {
        this.videos = videos;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<Taxonomy> getTaxonomy() {
        return this.taxonomy;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param taxonomy
     */
    public void setTaxonomy(Set<Taxonomy> taxonomy) {
        this.taxonomy = taxonomy;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<ConservationStatus> getConservationStatus() {
        return this.conservationStatus;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param conservationStatus
     */
    public void setConservationStatus(Set<ConservationStatus> conservationStatus) {
        this.conservationStatus = conservationStatus;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<Germplasm> getGermplasm() {
        return this.germplasm;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param germplasm
     */
    public void setGermplasm(Set<Germplasm> germplasm) {
        this.germplasm = germplasm;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String toString() {
        return "Organism {" + "id='" + id + '\'' + ", version='" + version + '\'' + ", name='" + name + '\'' + ", latinName='" + latinName + '\'' + ", initial='" + initial + '\'' + ", alias='" + alias + '\'' + ", namedInfo='" + namedInfo + '\'' + ", habitat='" + habitat + '\'' + ", endemism='" + endemism + '\'' + ", characteristic='" + characteristic + '\'' + ", useValues='" + useValues + '\'' + ", specimenSummary='" + specimenSummary + '\'' + ", creator='" + creator + '\'' + ", ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE + '\'' + ", ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE + '\'' + "}" + super.toString();
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param distributionToAdd
     */
    public void addToDistribution(Iterable<Distribution> distributionToAdd) {
        Assert.notNull(distributionToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (Distribution item : distributionToAdd) {
            this.distribution.add(item);
            item.setOrganism(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param distributionToRemove
     */
    public void removeFromDistribution(Iterable<Distribution> distributionToRemove) {
        Assert.notNull(distributionToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (Distribution item : distributionToRemove) {
            this.distribution.remove(item);
            item.setOrganism(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param specimensToAdd
     */
    public void addToSpecimens(Iterable<OrganismSpecimen> specimensToAdd) {
        Assert.notNull(specimensToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (OrganismSpecimen item : specimensToAdd) {
            this.specimens.add(item);
            item.setOrganism(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param specimensToRemove
     */
    public void removeFromSpecimens(Iterable<OrganismSpecimen> specimensToRemove) {
        Assert.notNull(specimensToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (OrganismSpecimen item : specimensToRemove) {
            this.specimens.remove(item);
            item.setOrganism(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param documentsToAdd
     */
    public void addToDocuments(Iterable<OrganismDocument> documentsToAdd) {
        Assert.notNull(documentsToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (OrganismDocument item : documentsToAdd) {
            this.documents.add(item);
            item.setOrganism(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param documentsToRemove
     */
    public void removeFromDocuments(Iterable<OrganismDocument> documentsToRemove) {
        Assert.notNull(documentsToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (OrganismDocument item : documentsToRemove) {
            this.documents.remove(item);
            item.setOrganism(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param docsToAdd
     */
    public void addToDocs(Iterable<Document> docsToAdd) {
        Assert.notNull(docsToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (Document item : docsToAdd) {
            this.docs.add(item);
            item.getOrganisms().add(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param docsToRemove
     */
    public void removeFromDocs(Iterable<Document> docsToRemove) {
        Assert.notNull(docsToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (Document item : docsToRemove) {
            this.docs.remove(item);
            item.getOrganisms().remove(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param imagesToAdd
     */
    public void addToImages(Iterable<OrganismImage> imagesToAdd) {
        Assert.notNull(imagesToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (OrganismImage item : imagesToAdd) {
            this.images.add(item);
            item.setOrganism(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param imagesToRemove
     */
    public void removeFromImages(Iterable<OrganismImage> imagesToRemove) {
        Assert.notNull(imagesToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (OrganismImage item : imagesToRemove) {
            this.images.remove(item);
            item.setOrganism(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param videosToAdd
     */
    public void addToVideos(Iterable<OrganismVideo> videosToAdd) {
        Assert.notNull(videosToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (OrganismVideo item : videosToAdd) {
            this.videos.add(item);
            item.setOrganism(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param videosToRemove
     */
    public void removeFromVideos(Iterable<OrganismVideo> videosToRemove) {
        Assert.notNull(videosToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (OrganismVideo item : videosToRemove) {
            this.videos.remove(item);
            item.setOrganism(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param taxonomyToAdd
     */
    public void addToTaxonomy(Iterable<Taxonomy> taxonomyToAdd) {
        Assert.notNull(taxonomyToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (Taxonomy item : taxonomyToAdd) {
            this.taxonomy.add(item);
            item.setOrganism(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param taxonomyToRemove
     */
    public void removeFromTaxonomy(Iterable<Taxonomy> taxonomyToRemove) {
        Assert.notNull(taxonomyToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (Taxonomy item : taxonomyToRemove) {
            this.taxonomy.remove(item);
            item.setOrganism(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param conservationStatusToAdd
     */
    public void addToConservationStatus(Iterable<ConservationStatus> conservationStatusToAdd) {
        Assert.notNull(conservationStatusToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (ConservationStatus item : conservationStatusToAdd) {
            this.conservationStatus.add(item);
            item.setOrganism(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param conservationStatusToRemove
     */
    public void removeFromConservationStatus(Iterable<ConservationStatus> conservationStatusToRemove) {
        Assert.notNull(conservationStatusToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (ConservationStatus item : conservationStatusToRemove) {
            this.conservationStatus.remove(item);
            item.setOrganism(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param germplasmToAdd
     */
    public void addToGermplasm(Iterable<Germplasm> germplasmToAdd) {
        Assert.notNull(germplasmToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (Germplasm item : germplasmToAdd) {
            this.germplasm.add(item);
            item.setOrganism(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param germplasmToRemove
     */
    public void removeFromGermplasm(Iterable<Germplasm> germplasmToRemove) {
        Assert.notNull(germplasmToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (Germplasm item : germplasmToRemove) {
            this.germplasm.remove(item);
            item.setOrganism(null);
        }
    }

    @PrePersist
    public void prePersist(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }

    @PreUpdate
    public void preUpdate(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }
}
